package com.bank.trade_records.service.impl;

import com.bank.trade_records.cun_money.entity.CunMoney;
import com.bank.trade_records.cun_money.service.CunMoneyService;
import com.bank.trade_records.entity.TradeRecords;
import com.bank.trade_records.qu_money.entity.QuMoney;
import com.bank.trade_records.qu_money.service.QuMoneyService;
import com.bank.trade_records.service.TradeRecordsService;
import com.bank.trade_records.zhuan_money.entity.ZhuanMoney;
import com.bank.trade_records.zhuan_money.service.ZhuanMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
//待优化，将大量重复方法封装；
@Service
public class TradeRecordsServiceImpl implements TradeRecordsService {
    @Autowired
    CunMoneyService cunMoneyService;
    @Autowired
    QuMoneyService quMoneyService;
    @Autowired
    ZhuanMoneyService zhuanMoneyService;
    public List<TradeRecords> getTransactionSummary(Long accountId) {
        List<TradeRecords> summaries = new ArrayList<>();

        // 获取存款记录  
        List<CunMoney> cunMoneyList = cunMoneyService.getCunMoneyListByAccountId(accountId);
        for (CunMoney cunMoney : cunMoneyList) {
            summaries.add(createTradeRecords(cunMoney));
        }

        // 获取取款记录  
        List<QuMoney> quMoneyList = quMoneyService.getQuMoneyListByAccountId(accountId);
        for (QuMoney quMoney : quMoneyList) {
            summaries.add(createTradeRecords(quMoney));
        }

        // 获取转账记录  
        List<ZhuanMoney> zhuanMoneyList = zhuanMoneyService.getZhuanMoneyListByAccountId(accountId);
        for (ZhuanMoney zhuanMoney : zhuanMoneyList) {
            summaries.add(createTradeRecords(zhuanMoney));
        }

        // 按交易日期排序  
        summaries.sort(Comparator.comparing(TradeRecords::getTransactionDate));

        // 控制返回的集合数据不超过十条
        if (summaries.size() > 10) {
            return summaries.subList(0, 10);
        } else {
            return summaries;
        }
    }
    private TradeRecords createTradeRecords(CunMoney record) {
        TradeRecords tradeRecords=new TradeRecords();
        tradeRecords.setAmount(record.getMoney());
        tradeRecords.setTransactionDate(record.getCreateTime());
        tradeRecords.setTransactionType("存款记录");
        return tradeRecords;
    }
    private TradeRecords createTradeRecords(QuMoney record) {
        TradeRecords tradeRecords=new TradeRecords();
        tradeRecords.setAmount(record.getMoney());
        tradeRecords.setTransactionDate(record.getCreateTime());
        tradeRecords.setTransactionType("取款记录");
        return tradeRecords;
    }
    private TradeRecords createTradeRecords(ZhuanMoney record) {
        TradeRecords tradeRecords=new TradeRecords();
        tradeRecords.setAmount(record.getMoney());
        tradeRecords.setTransactionDate(record.getCreateTime());
        tradeRecords.setTransactionType("转账记录");
        return tradeRecords;
    }
}
